package main

import (
	"fmt"
	"strconv"
	"strings"
)

func countAndSay(n int) string {
	p := "1"
	for i := 2; i <= n; i++ {
		cur := &strings.Builder{}
		for j, start := 0, 0; j < len(p); start = j {
			for j < len(p) && p[j] == p[start] {
				j++
			}
			cur.WriteString(strconv.Itoa(j - start))
			cur.WriteByte(p[start])
		}
		p = cur.String()
	}
	return p
}

func main() {
	fmt.Println("1211:  ", countAndSay(4))
}
